Inleiding
Een shell script heeft de .sh extensie.
De eerste regel is het absolute path naar de interpreter :
#!/bin/bash
Voor een systeem boot script is het aanbevolen de standaard shell te gebruiken :
#!/bin/sh
Een schell script heeft execute privileges nodig :
chmod +x script.sh
Uitvoeren :
./script.sh
Het ECHO commando
echo "Hallo Wereld"
-n Nieuwe regel overslaan
-e Toestaan sequenties:
| \0NNN | the character whose ASCII code is NNN (octal) |
| \\ | backslash |
| \a | alert (bell) |
| \b | backspace |
| \c | suppress trailing new line |
| \f | form feed |
| \n | new line |
| \r | carriage return |
| \t | horizontal tab |
| \v | vertical tab |
echo -n "Alles goed? [j/n]?"
echo -e "De Letter A : \0101"
echo -e "\aKolom 1 \tKolom 2 \tKolom 3"
Het READ commando
read naam
echo "Hallo $naam"
Invoer met timeout (10 seconden) :
read -t 10 -p "Naam : " naam
Invoer zonder af te beelden in Terminal :
read -s -p "Wachtwoord : " wachtwoord
Variabelen
naam="David van der Tuijn"
leeftijd=27
echo "Mijn naam is $naam en ik ben $leeftijd jaar oud."
Variabelen als argument
echo "$0 is de naam van het script"
echo "$1 is het eerste argument"
echo "$2 is het tweede argument"
echo "$3 is het derde argument"
echo "Alle argumenten zijn $* of $@"
Constante variabelen
declare -r naam=davidvandertuijn
Integer:
declare -i leeftijd=27
Globale shell variabelen
export naam=davidvandertuijn
echo $naam
Relationele Operatoren
| -eq | is equal to | 5 == 6 |
| -ne | is not equal to | 5 != 6 |
| -lt | is less than | 5 < 6 |
| -le | is less than or equal to | 5 <= 6 |
| -gt | is greater than | 5 > 6 |
| -ge | is greater than or equal to | 5 <= 6 |
Logische operatoren
| ! expressie | NOT |
| expressie1 -a expressie2 | AND |
| expressie1 -o expressie2 | OR |
Rekenkundige operatoren
| 1 + 2 | Optellen |
| 2 - 1 | Aftrekken |
| 10 - 5 | Delen |
| 20 % 3 | Modulo |
| 10 \* 3 | Vermenigvuldiging |
som=`expr 2 + 3`
echo "de som van 2 plus 3 = $som"
Bestand test operatoren
| -a file | True if file exists. |
| -b file | True if file exists and is a block special file. |
| -c file | True if file exists and is a character special file. |
| -d file | True if file exists and is a directory. |
| -e file | True if file exists. |
| -f file | True if file exists and is a regular file. |
| -g file | True if file exists and its set-group-id bit is set. |
| -h file | True if file exists and is a symbolic link. |
| -k file | True if file exists and its "sticky" bit is set. |
| -p file | True if file exists and is a named pipe (FIFO). |
| -r file | True if file exists and is readable. |
| -s file | True if file exists and has a size greater than zero. |
| -t fd | True if file descriptor fd is open and refers to a terminal. |
| -u file | True if file exists and its set-user-id bit is set. |
| -w file | True if file exists and is writable. |
| -x file | True if file exists and is executable. |
| -O file | True if file exists and is owned by the effective user id. |
| -G file | True if file exists and is owned by the effective group id. |
| -L file | True if file exists and is a symbolic link. |
| -S file | True if file exists and is a socket. |
| -N file | True if file exists and has been modified since it was last read. |
| file1 -nt file2 | True if file1 is newer (according to modification date) than file2. |
| file1 -ot file2 | True if file1 is older than file2. |
| file1 -ef file2 | True if file1 and file2 have the same device and inode numbers. |
BESTAND="$1"
if [ -f "$BESTAND" ]
then
echo "File exists."
else
echo "File does not exists!"
fi
If statement
getal=5
if [ $getal -lt 10 ]
then
echo "$getal is kleiner dan 10"
fi
If-else statement
if [ $1 -gt 0 ]
then
echo "$1 is positief"
else
echo "$1 is negatief"
fi
If-else statement (genesteld)
if [ $1 -lt 10 ]; then
echo "$1 is kleiner dan 10"
elif [ $1 -lt 20 ]; then
echo "$1 is groter dan 10 en kleiner dan 20"
fi
For loop
for ((i=0; i<=5; i++))
do
echo "Teller is $i"
done
While loop
i=0
while [ $i -le 10 ]
do
echo "Teller is $i"
i=`expr $i + 1`
done
Case statement
echo -n "Alles goed? [j/n]?"
read keuze
case $keuze in
"j") echo "Antwoord is JA";;
"n") echo "Antwoord is NEE";;
*) echo "Antwoord is onbekend";;
esac
Functie
datum()
{
echo `date +"%d %A %B %Y (%r)"`
}
echo "Datum : `datum`"
Met parameters :
leesplankje()
{
echo "$1 is de eerste parameter"
echo "$2 is de tweede parameter"
echo "$3 is de derde parameter"
}
echo "Leesplankje : `leesplankje aap noot mies`"
Exit Status
| Return waarde gelijk aan 0. | script is succesvol uitgevoerd. |
| Return waarde ongelijk aan 0. | script is niet succesvol uitgevoerd of een er is een fout opgetreden. |
if [ $# -lt 1 ]; then
exit 1
fi
De Exit Status wordt bewaard in $? :
echo $?
Meer informatie : Linux Shell Scripting Tutorial (LSST) v2.0